package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.common.annotation.Log;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysDept;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.enums.BusinessType;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.common.utils.SecurityUtils;
import com.huohuzhihui.common.utils.ServletUtils;
import com.huohuzhihui.common.utils.ip.AddressUtils;
import com.huohuzhihui.common.utils.ip.IpUtils;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerActivityBrowse;
import com.huohuzhihui.merchant.domain.MerCouponActivity;
import com.huohuzhihui.merchant.domain.MerMerchant;
import com.huohuzhihui.merchant.service.IMerActivityBrowseService;
import com.huohuzhihui.merchant.service.IMerCouponActivityService;
import com.huohuzhihui.merchant.service.IMerDouyinService;
import com.huohuzhihui.merchant.service.IMerMerchantService;
import com.huohuzhihui.system.service.ICloudStorageService;
import com.huohuzhihui.system.service.ISysDeptService;
import com.huohuzhihui.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 微信api
 *
 * @author lizhihe
 * @date 2020-11-22
 */
@RestController
@RequestMapping("/api/merchant")
public class ApiMerchantController extends BaseController {
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IMerDouyinService merDouyinService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询商户信息
     */
    @Log(title = "查询商户信息", businessType = BusinessType.DELETE)
    @GetMapping("/getMerchant")
    public AjaxResult getMerchant(Long id) {
        SysDept sysDept = this.sysDeptService.selectDeptById(id);
        return AjaxResult.success("查询商户信息成功", sysDept);
    }

    /**
     * 查询商户抖音信息
     *
     * @return
     */
    @GetMapping("/getMerchantDouyinInfo")
    public AjaxResult getMerchantDouyinInfo(Long merchantId) {
        return AjaxResult.success("查询商户抖音信息成功", merDouyinService.selectMerDouyinByDeptId(merchantId));
    }


    /**
     * 重置密码
     */
    @Log(title = "修改密码", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePwd")
    public AjaxResult updatePwd(String oldPassword, String newPassword) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String userName = loginUser.getUsername();
        String password = loginUser.getPassword();
        if (!SecurityUtils.matchesPassword(oldPassword, password)) {
            return AjaxResult.error("修改密码失败，旧密码错误");
        }
        if (SecurityUtils.matchesPassword(newPassword, password)) {
            return AjaxResult.error("新密码不能与旧密码相同");
        }
        if (userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword)) > 0) {
            // 更新缓存用户密码
            loginUser.getUser().setPassword(SecurityUtils.encryptPassword(newPassword));
            tokenService.setLoginUser(loginUser);
            return AjaxResult.success();
        }
        return AjaxResult.error("修改密码异常，请联系管理员");
    }
}
